package com.axend.aerosense.room.ui.fragment.vipfun;

import android.os.Bundle;
import android.view.View;
import android.widget.CompoundButton;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.axend.aerosense.room.entity.RoomVipSetBean;
import com.axend.aerosense.room.entity.VipFunSaveDataBean;
import com.axend.aerosense.room.ui.fragment.BottomNumberPickerDialog;
import com.jzxiang.pickerview.data.Type;
import com.axend.aerosense.common.UrlApi;
import com.axend.aerosense.room.R;
import com.axend.aerosense.room.databinding.RoomFragmentNightUpAnalysisBinding;

/**
 * 频繁起夜
 * Author: ChenHongWei
 * Date: 2021/3/24 14:25
 */
public class RoomNightUpAnalysisFragment extends BaseVipFunFragment<RoomFragmentNightUpAnalysisBinding> {

    public static RoomNightUpAnalysisFragment getInstance(){
        return new RoomNightUpAnalysisFragment();
    }

    @Override
    public int getLayoutId() {
        return R.layout.room_fragment_night_up_analysis;
    }

    @Override
    public int getBindingVariable() {
        return 0;
    }

    @Override
    protected void onRetryBtnClick() {

    }

    @Override
    public void onChange(CompoundButton buttonView, boolean isChecked) {
        super.onChange(buttonView, isChecked);
    }

    @Override
    protected void saveSettings() {
        super.saveSettings();
        VipFunSaveDataBean bean = new VipFunSaveDataBean(
                getRoomUuid(),
                getRoomType(),
                getCurrentActivity().getSwitchState(),
                getNum(viewDataBinding.roomAlarmTime.getText().toString()),
                0);
        viewModel.saveData(bean, UrlApi.URL.SetNightRise.getUrl());
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        getCurrentActivity().setVipFunTip(R.string.room_night_up_analysis_tips);
        setTitle(R.string.room_night_up_analysis);

        viewDataBinding.roomAlarmSetting.setOnClickListener(v -> {
            BottomNumberPickerDialog fragmentDialog = new BottomNumberPickerDialog.Builder()
                    .setType(Type.NUMBER)
                    .setMinValue(1)  //最小3
                    .setMaxValue(10)  //最大
                    .setDefValue(3)  //默认
                    .setUnit(getUnit())  //单位
                    .setWheelItemTextSize(16)
                    .setStep(1)  //步长
                    .setThemeColor(getResources().getColor(R.color.app_colorAccent))
                    .setCallBack((timePickerView, value) -> {
                        viewDataBinding.roomAlarmTime.setText(value +" "+ getUnit());
                        change();
                    })
                    .build();
            fragmentDialog.show(getChildFragmentManager(), fragmentDialog.getClass().getSimpleName());
        });

        RoomVipSetBean vipSettings = getVipSettings();
        if (vipSettings != null){
            getCurrentActivity().setSwitchChange(vipSettings.getFunctionSwitch() == 1);
            viewDataBinding.roomAlarmTime.setText(vipSettings.getFunctionRemind() + getUnit());
        }
    }

    private String getUnit(){
        return " "+getString(R.string.room_count);
    }

}
